package name.huzhenbo.java.threads;

class StoppableThread extends Thread {
    private boolean stopRequested = false;


    @Override
    public void run() {
        boolean done = false;

        while (!stopRequested && !done) {
            for (int i = 0; i < 10000; i++) {
                if (i == 9999) {
                    done = true;
                }
            }
        }
    }

    /**
     * This should be synchronized because of memory model issue.
     * <p/>
     * Otherwise the requestStop invocation from other thread cannot assure be seen by the running thread.
     */
    public synchronized void requestStop() {
        stopRequested = true;
    }
}
